package com.qf.service.impl;


import com.qf.dao.CartDao;
import com.qf.dao.OrderDao;
import com.qf.dao.impl.CartDaoImpl;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.pojo.*;
import com.qf.service.AddressService;
import com.qf.service.GoodsService;
import com.qf.service.OrderService;
import com.qf.service.OrderdetailService;

import java.util.*;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/18 17:26
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao=new OrderDaoImpl();
    private CartDao cartDao=new CartDaoImpl();
    private OrderdetailService orderdetailService=new OrderdetailServiceImpl();
    private GoodsService goodsService=new GoodsServiceImpl();
    private AddressService addressService=new AddressServiceImpl();

    @Override
    public Map creatOrder(Long uid, long aid, List<TbCart> cart) {
        long sum=0;
//        for (TbCart tbCart : cart) {
//            sum=tbCart.getGoodsList().getPrice()*tbCart.getNum();
//        }
        for (int i = 0; i < cart.size(); i++) {
            sum+=cart.get(i).getNum()*cart.get(i).getGoodsList().getPrice();
        }
//        生成一个订单id
        String orderId = UUID.randomUUID().toString().replace("-", "");
        orderDao.insertOrder(orderId,uid,sum,aid);

        for (int i = 0; i < cart.size(); i++) {
            TbCart tbCart = cart.get(i);
            orderDao.addOrderDetail(orderId,tbCart.getPid(),tbCart.getNum(),tbCart.getNum()*tbCart.getGoodsList().getPrice());
        }

//        清空购物车
        cartDao.clearCart(uid);

        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("id", orderId);
        resultMap.put("money", sum);

        return resultMap;
    }

    @Override
    public List<TbOrder> findOrder(Long uid) {
        List<TbOrder> tbOrder=orderDao.selectOrder(uid);
        if (tbOrder != null) {
            for (TbOrder order : tbOrder) {
//                OrderdetailService orderdetailService=new OrderdetailServiceImpl();
//                String orderId = order.getId();
//                List<TbOrderdetail> orderDetail=orderdetailService.findOrderDetail(orderId);
                TbAddress address=addressService.selectAddress(order.getAid());
                order.setAddress(address);
            }
        }
        return tbOrder;
    }


    @Override
    public TbOrder selectOrder(String orderId) {
        TbOrder order=orderDao.selectById(orderId);
        long aid = order.getAid();
        TbAddress address = addressService.selectAddress(aid);
        order.setAddress(address);

        List<TbOrderdetail> orderDetails = orderdetailService.findOrderDetail(orderId);
        for (TbOrderdetail orderDetail : orderDetails) {
            long pid = orderDetail.getPid();
            TbGoods tbGoods = goodsService.findByGid(pid);
            orderDetail.setGoods(tbGoods);
        }
        order.setOrderdetails(orderDetails);
        return order;
    }

    @Override
    public void udateStatus(String oid, int status) {
        orderDao.udateStatus(oid,status);
    }
}
